package info.sgu.ttdt.adapter;

import info.androidhive.slidingmenu.R;
import info.sgu.ttdt.BookMarkFragment;
import info.sgu.ttdt.model.BookMark;

import java.util.List;

import android.app.ListFragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ArrayAdapterItem extends ArrayAdapter<BookMark> {

	//private ArrayAdapterItem arrayAdapterItem = null;
	private Context context;
	private TextView txt1 = null;
	private TextView txt2 = null;
	private TextView txt3 = null;
	private Button btn_view = null;
	private Button btn_delete = null;
	private DatabaseHelper db;
	private ListFragment fragment;
	
	public ArrayAdapterItem(Context context, List<BookMark> objects, ListFragment fragment) {
		super(context, R.layout.list_item5, objects);
		//this.arrayAdapterItem = this;
	    this.context = context;
	    this.fragment = fragment;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View view = inflater.inflate(R.layout.list_item5, parent, false);

	    txt1 = (TextView) view.findViewById(R.id.text1);
	    txt2 = (TextView) view.findViewById(R.id.text2);
	    txt3 = (TextView) view.findViewById(R.id.text3);
	    btn_view = (Button) view.findViewById(R.id.button_view_bm);
	    btn_delete = (Button) view.findViewById(R.id.button_delete_bm);
	    db = new DatabaseHelper(context);

	    //final int i = position;
	    final BookMark bookMark = this.getItem(position);
	    txt1.setText("Mã sinh viên: " + bookMark.getMasinhvien());
	    txt2.setText("Tên sinh viên: " + bookMark.getTensinhvien());
	    txt3.setText("Loại: " + bookMark.getLoai());

	    btn_view.setOnClickListener(new OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	((BookMarkFragment)fragment).View(bookMark);
	        }
	    });
	    
	    btn_delete.setOnClickListener(new OnClickListener() {
	        @Override
	        public void onClick(View v) {
	            db.deleteBookmark(bookMark.getMasinhvien(), bookMark.getLoai());
	            ((BookMarkFragment)fragment).ReLoad();
	        }
	    });

	    return view;
	}

}
